﻿#region File Header
/***********************************************************************
 * Copyright © 2013 Beals Software
 * All Rights Reserved
************************************************************************
Author: Donald Beals
Date: February 4th, 2013
Description: TODO: Write a description of this file here.
****************************** Change Log ******************************
02.04.13 - Created initial file. (dbeals)
***********************************************************************/
#endregion

#region Using Statements
using System;
using Newtonsoft.Json;
#endregion

namespace GameJolt.Requests
{
	/// <summary>
	/// 
	/// </summary>
	internal static class ScoreRequestFactory
	{
		[JsonObject]
		internal sealed class ScoreRequestResult : IJsonResult<Score[]>
		{
			IJsonResponse<Score[]> IJsonResult<Score[]>.Response
			{
				get
				{
					return Response;
				}
			}

			[JsonProperty("response")]
			public ScoreRequestResponse Response
			{
				get;
				set;
			}
		}

		[JsonObject]
		internal sealed class ScoreRequestResponse : IJsonResponse<Score[]>
		{
			Score[] IJsonResponse<Score[]>.ResultSet
			{
				get
				{
					return Scores;
				}
			}

			public string WasSuccessful
			{
				get;
				set;
			}

			public string Message
			{
				get;
				set;
			}

			[JsonProperty("scores")]
			public Score[] Scores
			{
				get;
				set;
			}
		}

		[JsonObject]
		internal sealed class HighScoreTableRequestResult : IJsonResult<HighScoreTable[]>
		{
			IJsonResponse<HighScoreTable[]> IJsonResult<HighScoreTable[]>.Response
			{
				get
				{
					return Response;
				}
			}

			[JsonProperty("response")]
			public HighScoreTableRequestResponse Response
			{
				get;
				set;
			}
		}

		[JsonObject]
		internal sealed class HighScoreTableRequestResponse : IJsonResponse<HighScoreTable[]>
		{
			HighScoreTable[] IJsonResponse<HighScoreTable[]>.ResultSet
			{
				get
				{
					return Tables;
				}
			}

			[JsonProperty("success")]
			public string WasSuccessful
			{
				get;
				set;
			}

			[JsonProperty("message")]
			public string Message
			{
				get;
				set;
			}

			[JsonProperty("tables")]
			public HighScoreTable[] Tables
			{
				get;
				set;
			}
		}

		public static JsonRequest<Score[], ScoreRequestResult> CreateGetRequest(string gameID, string privateKey, string username, string userToken, int limit, string tableID)
		{
			var output = new JsonRequest<Score[], ScoreRequestResult>(gameID, privateKey, Constants.ScoreUrls.GetUrl);
			if(string.IsNullOrWhiteSpace(username) || string.IsNullOrWhiteSpace(userToken))
			{
				if(!string.IsNullOrWhiteSpace(username))
				{
					Logging.LogError("If you do not want user specific scores you cannot supply a username or a user token.");
					throw new Exception("User token is null, but username is not.");
				}
				else if(!string.IsNullOrWhiteSpace(userToken))
				{
					Logging.LogError("If you do not want user specific scores you cannot supply a username or a user token.");
					throw new Exception("Username is null, but user token is not.");
				}
			}
			else
			{
				output.Parameters.Add(RequestParameter.CreateUsername(username));
				output.Parameters.Add(RequestParameter.CreateUserToken(userToken));
			}

			if(limit > 0)
				output.Parameters.Add(RequestParameter.CreateLimit(limit));

			if(!string.IsNullOrWhiteSpace(tableID))
				output.Parameters.Add(RequestParameter.CreateTableID(tableID));

			return output;
		}

		public static StringRequest CreateAddRequest(string gameID, string privateKey, string score, int sort, string username, string userToken, string guest, string extraData, string tableID)
		{
			var output = new StringRequest(gameID, privateKey, Constants.ScoreUrls.AddUrl, RequestParameter.CreateScore(score), RequestParameter.CreateSort(sort));

			if(string.IsNullOrWhiteSpace(username) || string.IsNullOrWhiteSpace(userToken))
			{
				if(!string.IsNullOrWhiteSpace(username))
				{
					Logging.LogError("If you do not want user specific scores you cannot supply a username or a user token.");
					throw new Exception("User token is null, but username is not.");
				}
				else if(!string.IsNullOrWhiteSpace(userToken))
				{
					Logging.LogError("If you do not want user specific scores you cannot supply a username or a user token.");
					throw new Exception("Username is null, but user token is not.");
				}
				output.Parameters.Add(RequestParameter.CreateGuest(guest));
			}
			else
			{
				output.Parameters.Add(RequestParameter.CreateUsername(username));
				output.Parameters.Add(RequestParameter.CreateUserToken(userToken));
			}

			if(!string.IsNullOrWhiteSpace(extraData))
				output.Parameters.Add(RequestParameter.CreateExtraData(extraData));

			if(!string.IsNullOrWhiteSpace(tableID))
				output.Parameters.Add(RequestParameter.CreateTableID(tableID));

			return output;
		}

		public static JsonRequest<HighScoreTable[], HighScoreTableRequestResult> CreateGetTablesRequest(string gameID, string privateKey)
		{
			return new JsonRequest<HighScoreTable[], HighScoreTableRequestResult>(gameID, privateKey, Constants.ScoreUrls.TablesUrl);
		}
	}
}
